package com.changfeng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.changfeng.auth.annotation.AuthPower;
import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.ReatailersMenu;
import com.changfeng.bean.dto.bo.MenuTopBO;
import com.changfeng.bean.dto.vo.MenuTopVO;
import com.changfeng.bean.dto.vo.MenuVO;
import com.changfeng.common.CurrentUser;
import com.changfeng.dao.ReatailersMenuMapper;
import com.changfeng.dao.RetailersBannerMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class TestController {

    @Autowired
    private  RetailersBannerMapper retailersBannerMapper;

    @Autowired
    private ReatailersMenuMapper reatailersMenuMapper;

    @AuthPower
    @GetMapping("/test")
    public Object test() {

        return retailersBannerMapper.selectByPrimaryKey(1);

    }

    @RequestMapping("/login")
    public UserAuth login() {

        return CurrentUser.getCurrentUser();
    }

    @RequestMapping("/test2")
    public Integer test2() {
        throw new ChangFengException(ChangFengExceptionEnum.NO_POWER);
    }


    @GetMapping("/setMenu")
    public void setMenu() {
        ReatailersMenu reatailersMenu = new ReatailersMenu();
        reatailersMenu.setMenuName("网站管理");
        reatailersMenu.setPowerId(0);
        List<MenuVO> menuList = new ArrayList<>();
        MenuVO menu1 = new MenuVO();
        menu1.setName("轮播管理");
        menu1.setTo("/adminBannerList");
        menuList.add(menu1);

        String str = JSON.toJSONString(menuList);
        reatailersMenu.setMenuList(str);
        reatailersMenu.setGmtCreate(new Date());
        reatailersMenu.setGmtModified(new Date());

        reatailersMenuMapper.insert(reatailersMenu);

    }

    @GetMapping("/getMenu")
    public List<MenuTopVO>  getMenu() {
        List<MenuTopBO> topBOS = reatailersMenuMapper.findAll();
        List<MenuTopVO> topVOS = new ArrayList<>();
        for (MenuTopBO bo : topBOS) {
            MenuTopVO vo = new MenuTopVO();
            vo.setMenuName(bo.getMenuName());
            vo.setMenuList(JSONObject.parseArray(bo.getMenuList(),MenuVO.class));
            topVOS.add(vo);
        }

        System.out.println(topVOS);

        return topVOS;
    }
}
